package com.businessportal.web.controllers.calendar;

import com.businessportal.web.common.DateTimeHelper;
import com.businessportal.web.dataaccess.entities.Event;
import com.businessportal.web.dataaccess.entities.Login;
import com.businessportal.web.dataaccess.services.EventService;
import com.businessportal.web.dataaccess.services.UserService;
import org.json.simple.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;
import java.util.HashSet;

/**
 * Created by Vadim on 7/5/14.
 */
@WebServlet(urlPatterns = "/securepages/calendar_editor.do")
public class CalendarEditorController  extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try{
            req.setCharacterEncoding("UTF-8");

            String showEventId = req.getParameter("showEventId");

            HttpSession session = req.getSession();
            Login user = (Login)session.getAttribute("user");

            if(null != showEventId && !showEventId.isEmpty()){
                Event event = EventService.INSTANCE.getEventById(Long.parseLong(showEventId));

                req.setAttribute("event_id", event.getId());
                req.setAttribute("event_title", event.getTitle());
                req.setAttribute("is_public", event.isIs_public());
                req.setAttribute("start_date", DateTimeHelper.dateFormatForCalendar(event.getEvent_start_date()));
                req.setAttribute("end_date", DateTimeHelper.dateFormatForCalendar(event.getEvent_end_date()));
                req.setAttribute("event_color", event.getColor());
                req.setAttribute("event_description", event.getDescription());
                req.setAttribute("notify_me", event.isNotify_me());
                req.setAttribute("when_notify_me", event.getWhen_notify_me());

            } else {
                req.setAttribute("event_id", 0);
                req.setAttribute("event_title", "");
                req.setAttribute("is_public", true);
                req.setAttribute("start_date", "");
                req.setAttribute("end_date", "");
                req.setAttribute("event_color", "");
                req.setAttribute("event_description", "");
                req.setAttribute("notify_me", true);
                req.setAttribute("when_notify_me", "");
            }

            req.setAttribute("events", user.getEvents());

        } catch(Exception e) {
            req.setAttribute("errors", e.getMessage());
            req.setAttribute("events", new HashSet<Event>());
            req.setAttribute("event_id", 0);
            req.setAttribute("event_title", "");
            req.setAttribute("is_public", true);
            req.setAttribute("start_date", "");
            req.setAttribute("end_date", "");
            req.setAttribute("event_color", "");
            req.setAttribute("event_description", "");
            req.setAttribute("notify_me", true);
            req.setAttribute("when_notify_me", "");

        }

        getServletContext().getRequestDispatcher("/securepages/calendar_creator.jsp").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        resp.setContentType("application/json");
        JSONObject retVal=new JSONObject();

        try {
            req.setCharacterEncoding("UTF-8");

            Long event_id = Long.parseLong(req.getParameter("event_id"));
            Date start_date = DateTimeHelper.parseDateFromString(req.getParameter("start_date"));
            Date end_date = DateTimeHelper.parseDateFromString(req.getParameter("end_date"));
            Boolean is_public = Boolean.parseBoolean(req.getParameter("is_public"));
            Boolean notify_me = Boolean.parseBoolean(req.getParameter("notify_me"));
            Date when_notify_me = DateTimeHelper.parseDateFromString(req.getParameter("when_notify_me"));

            HttpSession session = req.getSession();
            Login user = (Login)session.getAttribute("user");

            if(null != event_id && 0 != event_id){
                Event event = EventService.INSTANCE.getEventById(event_id);

                event.setIs_public(is_public);
                event.setNotify_me(notify_me);
                event.setWhen_notify_me(when_notify_me);
                event.setEvent_start_date(start_date);
                event.setEvent_end_date(end_date);

                EventService.INSTANCE.Update(event);

                retVal.put("message", "OK");
            } else {
                retVal.put("message", "Not found");
            }

        } catch (Exception e) {
            retVal.put("error", e.getMessage());
        }

        resp.getWriter().write(retVal.toString());
    }
}
